package com.zahb.qadx.ui.activity;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.model.CommentContentBean;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.adapter.CommentTeacherAdapter;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.Tips;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;


public class CommentTeacherActivity extends BaseActivity {

    RecyclerView recyc;

    TextView tvComment,tvName;

    List<CommentContentBean> list = new ArrayList<CommentContentBean>();
    CommentTeacherAdapter adapter = new CommentTeacherAdapter(R.layout.item_comment_teacher,list);

    boolean isCommented = false;
    int classId = 0;
    String className;

    @Override
    protected int getLayout() {
        return R.layout.activity_comment_teacher;
    }

    @Override
    protected void initViews() {

        isCommented = getIntent().getBooleanExtra("type",false);
        classId = getIntent().getIntExtra("data",0);
        className = getIntent().getStringExtra("datatwo");


        recyc = findViewById(R.id.recyc);
        tvComment = findViewById(R.id.tvComment);
        tvName = findViewById(R.id.tvName);

        tvName.setText(className);



        list.add(new CommentContentBean("板书/课件满意度？"));
        list.add(new CommentContentBean("您觉得内容适宜性怎么样？"));
        list.add(new CommentContentBean("您觉得老师的教学方法如何？"));
        list.add(new CommentContentBean("您觉得老师的教学态度如何？"));
        list.add(new CommentContentBean("您觉得老师的教学效果如何？"));

        recyc.setAdapter(adapter);
        recyc.setLayoutManager(new LinearLayoutManager(this));

        if(isCommented){
            adapter.mClickable = false;
            getClassStuEvaluation();
            tvComment.setText("关闭");
            tvComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }else{
            tvComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (CommentContentBean bean : list){
                        if(bean.level==0){
                            //ToastUtils.s(CommentTeacherActivity.this,"请完成全部教师评论项后再提交");
                            showBindToast("请完成全部教师评论项后再提交");
                            return;
                        }
                    }
                    saveClassStuEvaluation();
                }
            });
        }

    }
    private Gson mGson = new Gson();

    private void getClassStuEvaluation(){
        if(BaseApplication.getContext().getDataLogin()==null){
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("appKey", Constant.TRAIN_APP_KEY);
        params.put("orgId", BaseApplication.getContext().getDataLogin().getCurrentOrg());
        params.put("classId", classId);
        params.put("userID", BaseApplication.getContext().getDataLogin().getUser().getId());

        String json = mGson.toJson(params);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        addDisposable(RetrofitService.getNetService()
                .getClassStuEvaluation(classId+"",BaseApplication.getContext().getDataLogin().getUser().getId()+"")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        list.get(0).level= response.getData().getSatisfaction();
                        list.get(1).level= response.getData().getContentSuitability();
                        list.get(2).level= response.getData().getTeachingMethod();
                        list.get(3).level= response.getData().getTeachingAttitude();
                        list.get(4).level= response.getData().getTeachingEffectiveness();
                        adapter.notifyDataSetChanged();

                    }
                }, throwable -> {
                    Tips.RequestError(getActivity());
                    throwable.printStackTrace();
                }));
    }

    private void saveClassStuEvaluation(){
        if(BaseApplication.getContext().getDataLogin()==null){
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("appKey", Constant.TRAIN_APP_KEY);
        params.put("orgId", BaseApplication.getContext().getDataLogin().getCurrentOrg());
        params.put("classId", classId);
        params.put("userId", BaseApplication.getContext().getDataLogin().getUser().getId());

        params.put("satisfaction", list.get(0).level);
        params.put("contentSuitability", list.get(1).level);
        params.put("teachingMethod", list.get(2).level);
        params.put("teachingAttitude", list.get(3).level);
        params.put("teachingEffectiveness", list.get(4).level);
        params.put("creator", BaseApplication.getContext().getDataLogin().getUser().getId());
        params.put("createTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));


        String json = mGson.toJson(params);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        addDisposable(RetrofitService.getNetService()
                .saveClassStuEvaluation(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        //ToastUtils.s(CommentTeacherActivity.this,"评论成功");
                        showBindToast("评论成功");
                        setResult(Activity.RESULT_OK);
                        finish();
                    }
                }, throwable -> {
                    Tips.RequestError(getActivity());
                    throwable.printStackTrace();
                }));
    }
    @Override
    protected int getTitleStringRes() {
        return R.string.comment_teacher;
    }
}
